package leetCode;

import java.util.ArrayList;
import java.util.List;

public class t54{
    public static void main(String[] args) {
        int[][] matrix={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        Solution.spiralOrder(matrix);
    }
    static class Solution {
        public  static List<Integer> spiralOrder(int[][] matrix) {
            List<Integer> ret=new ArrayList<>();
            int m=matrix.length;
            int n=matrix[0].length;
            int top=0;
            int bottom=m;
            int left=0;
            int right=n;
            int num=1;
            while(left<right){
                for (int i=left;i<right;i++){
                    ret.add(matrix[top][i]);
                }
                top++;
                if (top>=bottom)break;
                for (int i=top;i<bottom;i++){
                    ret.add(matrix[i][right-1]);
                }
                right--;
                if (right<=left)break;
                for (int i=right-1;i>=left;i--){
                    ret.add(matrix[bottom-1][i]);
                }
                bottom--;
                if (top>=bottom)break;
                for (int i=bottom-1;i>=top;i--){
                    ret.add(matrix[i][left]);
                }
                left++;
                if (right<=left)break;
            }
            return ret;
        }
    }
}
